﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Comments_session
/// </summary>
public class Comments_session
{
    private static SqlConnection sqlConnection;

    public static SqlDataAdapter sqlAdapter;
    private static DataTable objDataTable;
   // public static SqlDataAdapter sqlAdapter;
    public static SqlCommand sqlCommand;  
	public Comments_session()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static DataTable Get_CommentsAsTable(string whereSql, string orderBy)
    {
        DataTable dt = new DataTable(); // chu y cho nay nek
        dt = DataProvider.GetDataTable("select * from Comments where  " + whereSql + " order by " + orderBy); // giam dan <=> moi truoc
        return dt;
    }

    public static void Insert_Comments(Comments comment)
    {
        try
        {
            sqlConnection = DataProvider.CreateConnection();
            //================================== Thỏa mãn các điều kiện và thêm vào ============================//
            DataTable datatable = new DataTable();
            sqlAdapter = new SqlDataAdapter("select * from Comments", sqlConnection);
            sqlAdapter.Fill(datatable);

            DataRow datarow = datatable.NewRow();
            datarow[0] = comment.Pro_ID;// ma the loai
            datarow[2] = comment.Com_Desc;// ten the loai
            datarow[3] = comment.Com_Title;// ma the loai
            datarow[4] = comment.Com_Status;// ten the loai
            datarow[5] = comment.User_ID;// ma the loai user id tu tu lam cho nay


            datatable.Rows.Add(datarow);

            sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;
            sqlCommand.CommandText = "INSERT INTO Comments VALUES(@Pro_ID,@Com_Desc,@Com_Title,@Com_Status,@User_ID)";
            sqlCommand.Parameters.Add("@Pro_ID", SqlDbType.Int, 10, "Pro_ID");
            sqlCommand.Parameters.Add("@Com_Desc", SqlDbType.NVarChar, 150, "Com_Desc");
            sqlCommand.Parameters.Add("@Com_Title", SqlDbType.NVarChar, 150, "Com_Title");
            sqlCommand.Parameters.Add("@Com_Status", SqlDbType.Bit, 1, "Com_Status");
            sqlCommand.Parameters.Add("@User_ID", SqlDbType.Int, 10, "User_ID");

            sqlAdapter.InsertCommand = sqlCommand;
            sqlAdapter.Update(datatable);
            sqlCommand.Dispose();
            sqlCommand = null;
        }catch(Exception e){
            WebMsgBox.Show("ko co insert duoc");
        }
       
    }
    public static void Delete_Comments_Where(string whereSql)
    {
        try
        {
            sqlConnection = new SqlConnection();

            String strSQL = "select * from Comments where   " + whereSql;
            sqlConnection = DataProvider.CreateConnection();
            SqlDataAdapter sqlAdapter = new SqlDataAdapter();

            SqlCommand objcommand = new SqlCommand();
            objcommand.Connection = sqlConnection;
            objcommand.CommandType = CommandType.Text;
            objcommand.CommandText = strSQL;

            sqlAdapter.SelectCommand = objcommand;
            objDataTable = new DataTable("");
            sqlAdapter.Fill(objDataTable);
            for (int i = 0; i < objDataTable.Rows.Count; i++)
            {
                objDataTable.Rows[i].Delete();
                sqlAdapter.Update(objDataTable);
            }
            sqlAdapter.Update(objDataTable);
            sqlConnection.Close();
            sqlConnection.Dispose();
            sqlConnection = null;
        }
        catch (Exception e) { WebMsgBox.Show("ngoai le comment"); }
    }
}
